package com.adminserver.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.adminserver.customEntity.AnnouncementEntity;
import com.adminserver.util.RedisUtil;
import com.adminserver.util.responseUtil.ResponseEnum;
import com.adminserver.util.responseUtil.ResponseUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import java.util.*;

@RestController
@RequestMapping("/announcement")
public class AnnouncementController {

    static Jedis redis = RedisUtil.getRedis();

    public static final String ALL = "all";
    public static final String USER = "user";
    public static final String WORKER = "worker";

    public static final HashMap<String, String> ANNOUNCEMENT_RANGE = new HashMap<>();

    static {
        ANNOUNCEMENT_RANGE.put(ALL, "announcement:all");
        ANNOUNCEMENT_RANGE.put(USER, "announcement:user");
        ANNOUNCEMENT_RANGE.put(WORKER, "announcement:worker");
    }

    /**
     * 格式化从redis中获取的公告对象信息
     * @param range 范围
     * @return
     */
    public static ArrayList<AnnouncementEntity> formatRedisAnnouncement(String range) {
        Map<String, String> map = redis.hgetAll(ANNOUNCEMENT_RANGE.get(range));
        Set<String> keySet = map.keySet();
        ArrayList<AnnouncementEntity> list = new ArrayList<>();
        for (String key : keySet) {
            list.add(JSON.parseObject(map.get(key), AnnouncementEntity.class));
        }
        return list;
    }

    @GetMapping("/pullAllAnnouncement")
    public ResponseUtil pullAllAnnouncement() {
        Map<String, List<AnnouncementEntity>> result = new HashMap();
        if (redis.exists(ANNOUNCEMENT_RANGE.get(ALL))) {
            result.put(ALL, formatRedisAnnouncement(ALL));
        }
        if (redis.exists(ANNOUNCEMENT_RANGE.get(USER))) {
            result.put(USER, formatRedisAnnouncement(USER));
        }
        if (redis.exists(ANNOUNCEMENT_RANGE.get(WORKER))) {
            result.put(WORKER, formatRedisAnnouncement(WORKER));
        }
        if (!result.isEmpty()) return ResponseUtil.setE(ResponseEnum.OK).setData(result);
        return ResponseUtil.setE(ResponseEnum.FAIL).setMsg("暂无公告");
    }

    @PostMapping("/issueAnnouncement")
    public ResponseUtil issueAnnouncement(@RequestBody AnnouncementEntity announcement) {
        announcement.setId(IdUtil.simpleUUID());
        redis.hset(ANNOUNCEMENT_RANGE.get(announcement.getAlertRange()), announcement.getId(), JSON.toJSONString(announcement));
        return ResponseUtil.setE(ResponseEnum.OK).setMsg("新公告【" + announcement.getTitle() + "】已发布");
    }

    @PostMapping("/delAnnouncement")
    public ResponseUtil delAnnouncement(@RequestBody AnnouncementEntity announcement) {
        if (ObjectUtil.isEmpty(announcement)) return ResponseUtil.setE(ResponseEnum.FAIL).setMsg("公告删除失败");
        redis.hdel(ANNOUNCEMENT_RANGE.get(announcement.getAlertRange()), announcement.getId());
        return ResponseUtil.setE(ResponseEnum.OK);
    }

}
